package uy.com.nfc.writer;

import java.util.List;

import uy.com.nfc.writer.adapters.LocalListAdapter;
import uy.com.nfc.writer.writer.TagWriter;
import android.app.Activity;
import android.app.PendingIntent;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Typeface;
import android.nfc.NfcAdapter;
import android.nfc.Tag;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.nfctagcommons.classes.VigilancePoint;
import com.example.nfctagcommons.dialog.ConnectivityErrorDialog;
import com.example.nfctagcommons.dialog.LoadOnBackgroundDialog;
import com.example.nfctagcommons.dialog.LoadOnBackgroundDialog.IBackgroundAction;
import com.example.nfctagcommons.exceptions.ConnectivityException;
import com.example.nfctagcommons.managers.LocalManager;

public class MainActivity extends Activity {
	private NfcAdapter mNfcAdapter;
	private IntentFilter[] mWriteTagFilters;
	private PendingIntent mNfcPendingIntent;
	private String localId;
	private List<VigilancePoint> localList;
	private Typeface customTypeface;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		customTypeface = Typeface.createFromAsset(getAssets(), "fonts/AliquamREG.ttf");
		TextView mainTitle = (TextView) findViewById(R.id.main_title);
		mainTitle.setTypeface(customTypeface);
		setTitle(R.string.app_name); 
		getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
		mNfcAdapter = NfcAdapter.getDefaultAdapter(this);
		mNfcPendingIntent = PendingIntent.getActivity(this, 0, new Intent(this,getClass()).addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_CLEAR_TOP), 0);
		IntentFilter discovery = new IntentFilter(NfcAdapter.ACTION_TAG_DISCOVERED);
		IntentFilter ndefDetected = new IntentFilter(NfcAdapter.ACTION_NDEF_DISCOVERED);
		IntentFilter techDetected = new IntentFilter(NfcAdapter.ACTION_TECH_DISCOVERED);
		mWriteTagFilters = new IntentFilter[] { discovery, ndefDetected }; 
		
		LoadOnBackgroundDialog background = new LoadOnBackgroundDialog(this);
		background.load(false, "Obteniendo lista de puntos...", new IBackgroundAction() {			
			@Override
			public boolean doBackgroundAction() { 
				try {
					localList = LocalManager.getAllLocals(MainActivity.this);
					return true;
				} catch (ConnectivityException e) {
					e.printStackTrace();
				}
				return false;
			}
			@Override
			public void doPostExecuteAction(boolean success) {
				if (success){
					String[] items = new String[localList.size()];
					ListView localListView = (ListView)findViewById(R.id.localList);
					localListView.setAdapter(new LocalListAdapter(MainActivity.this,items,localList));
				}else{
					ConnectivityErrorDialog.showDialog(MainActivity.this, MainActivity.class);
				}
			}
			@Override
			public void onCancelled() {
			}
		});
	}
	@Override
	protected void onResume() {
		super.onResume();
		if (mNfcAdapter != null) {
			ImageView ncStatusIcon = (ImageView)findViewById(R.id.nfcStatusIcon);
			LinearLayout nfcStatusBar = (LinearLayout)findViewById(R.id.nfcStatusBar);
			if (mNfcAdapter.isEnabled()) { 
				ncStatusIcon.setVisibility(View.VISIBLE);
				nfcStatusBar.setVisibility(View.GONE);
			}else{
				ncStatusIcon.setVisibility(View.INVISIBLE);
				nfcStatusBar.setVisibility(View.VISIBLE);
			}
			mNfcAdapter.enableForegroundDispatch(this, mNfcPendingIntent, mWriteTagFilters, null);
		} else {
			Toast.makeText(this, "NO HAY ADAPTADOR NFC.", Toast.LENGTH_SHORT).show();
		}
	}

	@Override
	protected void onPause() {
		super.onPause();
		if (mNfcAdapter != null)
			mNfcAdapter.disableForegroundDispatch(this);
	}

	@Override
	protected void onNewIntent(Intent intent) {
		super.onNewIntent(intent);
		if (NfcAdapter.ACTION_TAG_DISCOVERED.equals(intent.getAction())) {
			if (localId == null){
				Toast.makeText(this, "ERROR: Seleccionar punto", Toast.LENGTH_SHORT).show();
				return;
			}
			Tag detectedTag = intent.getParcelableExtra(NfcAdapter.EXTRA_TAG);
			try {				
				TagWriter.write(this, detectedTag, localId);
			} catch (Exception e) {
				e.printStackTrace();
			}
			ListView localListView = (ListView)findViewById(R.id.localList);
			localListView.invalidateViews();
			localId = null;
		}
	}

	public void setLocal(String id) {
		localId = id;
	}
}
